<?php

require_once 'lib.php';

/**
 * Description of ExamsRetriever
 *
 * @author markus
 */
class ExamsRetriever {
    private $connection;
    private $url;
    private $result;
    
    function __construct(ActionsConnection $connection, $locale = "de"){
        $this->url = "https://actions.fh-joanneum.at/exams/administration.do?locale=".$locale;
        $this->connection = $connection;
        
        logger("Exams", $this->connection->status, $this->connection->key);
    }
    
    public function retrieve(){
        $e = curl_init($this->url);
        curl_setopt($e, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($e, CURLOPT_COOKIEFILE, $this->connection->getFile());
        curl_setopt($e, CURLOPT_COOKIEJAR, $this->connection->getFile());
        curl_setopt($e, CURLOPT_HEADER, 0);

        $content = curl_exec($e);

        curl_close($e);

        $this->result = $content;
    }
    
    public function toXml(){
        $content = $this->result;
        
        // Parse HTML result
        $content = explode("examsTable", $content);
        $content = '<table cellpadding="3" cellspacing="0" class="list" id="'.$content[1];

        $content = explode('</table>', $content);

        $content = trim($content[0]).'</table>';

        $content = str_replace("&nbsp;", "", $content);
        $content = str_replace("\t", "", $content);
        $content = str_replace("\n", "", $content);
        $content = str_replace("\r", "", $content);
        $content = str_replace("</td></td>", "</td>", $content);
        $content = str_replace("&Uuml;", "Ü", $content);
        $content = str_replace("&uuml;", "ü", $content);
        $content = str_replace("&Auml;", "Ä", $content);
        $content = str_replace("&auml;", "ä", $content);
        $content = str_replace("&Ouml;", "Ö", $content);
        $content = str_replace("&ouml;", "ö", $content);
        $content = str_replace("&szlig;", "ß", $content);
        $content = str_replace("&", "und", $content);
        $content = str_replace("<br />", " ", $content);

        $content = '<?xml version="1.0"?>'.$content;

        $content = strip_tags($content, "<input><table><tr><td><th>");

        $xmlObj = simplexml_load_string($content);
        $fmarr = objectsIntoArray($xmlObj);
        
        $aktuell = $fmarr['tr'];
        
        $exams = "";

        foreach($aktuell as $exam){
            $tid = $exam['td'][0]['input']['@attributes']['name'];
            $tfach = $exam['td'][1];
            $ttyp = $exam['td'][2];
            $tmodus = $exam['td'][5];
            $tdatum = $exam['td'][7];
            $tbis = $exam['td'][9];
            $tstatus = explode(" ", $exam['@attributes']['class']);
            $tstatus = $tstatus[1];
            
            $tdatumstamp = explode("-", $tdatum);
            $tdatumstamp = strtotime($tdatumstamp[0]);
            $tbisstamp = strtotime($tbis);
            
            //var_dump($tstatus);

            if($tfach != null && $tbis != null){
                $exams[$key][] = array($tfach, $ttyp, $tmodus, $tdatum, $tbis, $tdatumstamp, $tbisstamp, $tstatus, $tid);
            }

            if($exam['th'] != '' && !is_array($exam['th'])){
                $key = $exam['th'];
            }
	}

        // Generate XML output
        $result = '<?xml version="1.0"?>';
        $result .= '<Overview>';
        $result .= '<Status>'.$this->connection->status.'</Status>';

        unset($exam);

        $terms = array_keys($exams);
        foreach($terms as $term){   
        $result .= '<Term name="'.$term.'">';
            foreach($exams[$term] as $exam){
                if($exam[7] != "takenPlace"){
                    $result .= '<Exam>';
                        $result .= '<Id>'.$exam[8].'</Id>';
                        $result .= '<Title>'.$exam[0].'</Title>';
                        $result .= '<Type>'.$exam[1].'</Type>';
                        $result .= '<Mode>'.$exam[2].'</Mode>';
                        $result .= '<Date>'.$exam[3].'</Date>';
                        $result .= '<RegistrationEnd>'.$exam[4].'</RegistrationEnd>';
                        $result .= '<DateUnix>'.$exam[5].'</DateUnix>';
                        $result .= '<RegistrationEndUnix>'.$exam[6].'</RegistrationEndUnix>';
                        $result .= '<ExamStatus>'.$exam[7].'</ExamStatus>';
                    $result .= '</Exam>';
                }
            }
        $result .= '</Term>';
        }
        $result .= '</Overview>';
        
        return $result;
    }
}

?>
